package com.zxl.soft.generator.common.model;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.cron.CronException;
import lombok.Data;

import java.util.Collection;
import java.util.Map;
import java.util.stream.Collectors;

@Data
public class PatternCron {


    /**
     * 秒
     */
    private String second = "";
    /**
     * 分钟
     */
    private String minute = "";
    /**
     * 小时
     */
    private String hourOfDay = "";
    /**
     * 日
     */
    private String dayOfMonth = "";
    /**
     * 月
     */
    private String month = "";
    /**
     * 周
     */
    private String dayOfWeek = "";
    /**
     * 年
     */
    private String year = "";




    /**
     * 解析对象为表达式
     *
     * @param patternCron
     * @return
     */
    public String parseTheObjectAsAnExpression(PatternCron patternCron) {
        Map<String, Object> map = BeanUtil.beanToMap(patternCron);
        Collection<Object> values = map.values();
        String collect = values.stream().map(x -> x + "").collect(Collectors.joining(" "));
        return collect;
    }

    /**
     * 解析表达式为对象
     *
     * @param pattern 表达式
     */
    public void analyzeExpressionsAsObjects(String pattern) {


        final String[] parts = pattern.split("\\s");

        // 偏移量用于兼容Quartz表达式，当表达式有6或7项时，第一项为秒
        int offset = 0;
        if (parts.length == 6 || parts.length == 7) {
            offset = 1;
        } else if (parts.length != 5) {
            throw new CronException("Pattern [{}] is invalid, it must be 5-7 parts!", pattern);
        }


        try {
            // 秒
            if (1 == offset) {// 支持秒的表达式
                this.second = parts[0];
            }
        } catch (Exception e) {
            throw new CronException(e, "Invalid pattern [{}], parsing 'second' field error!", pattern);
        }
        // 分
        try {
            this.minute = parts[offset];
        } catch (Exception e) {
            throw new CronException(e, "Invalid pattern [{}], parsing 'minute' field error!", pattern);
        }
        // 小时
        try {

            this.hourOfDay = parts[1 + offset];

        } catch (Exception e) {
            throw new CronException(e, "Invalid pattern [{}], parsing 'hour' field error!", pattern);
        }
        // 每月第几天
        try {
            this.dayOfMonth = parts[2 + offset];
        } catch (Exception e) {
            throw new CronException(e, "Invalid pattern [{}], parsing 'day of month' field error!", pattern);
        }
        // 月
        try {
            this.month = parts[3 + offset];

        } catch (Exception e) {
            throw new CronException(e, "Invalid pattern [{}], parsing 'month' field error!", pattern);
        }
        // 星期几
        try {
            this.dayOfWeek = parts[4 + offset];

        } catch (Exception e) {
            throw new CronException(e, "Invalid pattern [{}], parsing 'day of week' field error!", pattern);
        }
        // 年
        if (parts.length == 7) {// 支持年的表达式
            try {
                this.year = parts[6 + offset];
            } catch (Exception e) {
                throw new CronException(e, "Invalid pattern [{}], parsing 'year' field error!", pattern);
            }
        }
    }
}
